<?php
//声明命名空间
namespace app\admin\controller;
use app\admin\model\User;
use think\Session;

/**
 * Class Login
 * @package app\admin\controller
 */
class Login extends Base {
	/**
	 * @Author   This
	 * @DateTime 2018-09-20
     * @return mixed
	 */
	public function index(){
		$this->assign('site_title','首页-后台登录-百步园艺');
		return $this->fetch('login');
	}

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
	public function login(){
	    if(!request()->isPost()){
	        ajaxReturn(0,'非法传参！',-1);
        }
        if(!$_POST['username'] || !$_POST['password']){
            ajaxReturn(0,'用户名或密码不能为空！',0);
        }
        $username = $_POST['username'];
        $password = $_POST['password'];
        //随机生成六位字符串
        $model = new User;
        $data = $model->getOne(array('user_name'=>$username));
        if(empty($data)){
            ajaxReturn(0,'用户名不存在！',0);
        }else{
            $password = md5(md5($password.$data['user_salt']));
            if($password != $data['user_password']){
                ajaxReturn(0,'密码不正确！',0);
            }
            Session::set('username',$username);
            Session::set('remark',$data['remark']);
            Session::set('user_id',$data['user_id']);
            ajaxReturn(1,'登录成功！',1);
        }
    }

    public function logout(){
	    Session::delete('username');
	    Session::delete('remark');
	    Session::delete('user_id');
	    echo "<script type='text/javascript' src='http://{$_SERVER['HTTP_HOST']}/baibu/public/static/js/jquery3.2.js'></script><script type='text/javascript' src='http://{$_SERVER['HTTP_HOST']}/baibu/public/static/layer/layer.js'></script><script>layer.msg('退出成功!',{icon:1,time:1200,end:function() {
  location.href='http://'+window.location.host+'/baibu/admin/Login/';
}})</script>";
    }
}